import { defineStore } from 'pinia'

export const useDiaryStore = defineStore('diary', {
  state: () => ({
    diaries: [],
    categories: [],
    tags: [],
    currentDiary: null,
    loading: false,
    hasMore: true,
    currentPage: 1,
    pageSize: 10,
    // 编辑状态
    editingDiaryId: null,
    editingDate: null,
    filters: {
      categoryId: null,
      tagIds: [],
      startDate: null,
      endDate: null,
      keyword: '',
      mood: null
    }
  }),
  
  getters: {
    // 获取分类映射
    categoryMap: (state) => {
      const map = {}
      state.categories.forEach(category => {
        map[category.id] = category
      })
      return map
    },
    
    // 获取标签映射
    tagMap: (state) => {
      const map = {}
      state.tags.forEach(tag => {
        map[tag.id] = tag
      })
      return map
    },
    
    // 筛选后的日记列表
    filteredDiaries: (state) => {
      let filtered = [...state.diaries]
      
      // 按分类筛选
      if (state.filters.categoryId) {
        filtered = filtered.filter(diary => diary.categoryId === state.filters.categoryId)
      }
      
      // 按标签筛选
      if (state.filters.tagIds.length > 0) {
        filtered = filtered.filter(diary => {
          return diary.tagIds?.some(tagId => state.filters.tagIds.includes(tagId))
        })
      }
      
      // 按关键词筛选
      if (state.filters.keyword) {
        const keyword = state.filters.keyword.toLowerCase()
        filtered = filtered.filter(diary => {
          return diary.title.toLowerCase().includes(keyword) ||
                 diary.content.toLowerCase().includes(keyword)
        })
      }
      
      // 按心情筛选
      if (state.filters.mood) {
        filtered = filtered.filter(diary => diary.mood === state.filters.mood)
      }
      
      // 按日期筛选
      if (state.filters.startDate) {
        filtered = filtered.filter(diary => diary.diaryDate >= state.filters.startDate)
      }
      
      if (state.filters.endDate) {
        filtered = filtered.filter(diary => diary.diaryDate <= state.filters.endDate)
      }
      
      return filtered
    }
  },
  
  actions: {
    // 设置日记列表
    setDiaries(diaries, isLoadMore = false) {
      if (isLoadMore) {
        this.diaries = [...this.diaries, ...diaries]
      } else {
        this.diaries = diaries
      }
    },
    
    // 设置分类列表
    setCategories(categories) {
      this.categories = categories
    },
    
    // 设置标签列表
    setTags(tags) {
      this.tags = tags
    },
    
    // 设置当前日记
    setCurrentDiary(diary) {
      this.currentDiary = diary
    },
    
    // 添加日记
    addDiary(diary) {
      this.diaries.unshift(diary)
    },
    
    // 更新日记
    updateDiary(updatedDiary) {
      const index = this.diaries.findIndex(diary => diary.id === updatedDiary.id)
      if (index !== -1) {
        this.diaries[index] = updatedDiary
      }
      
      if (this.currentDiary && this.currentDiary.id === updatedDiary.id) {
        this.currentDiary = updatedDiary
      }
    },
    
    // 删除日记
    deleteDiary(diaryId) {
      this.diaries = this.diaries.filter(diary => diary.id !== diaryId)
      
      if (this.currentDiary && this.currentDiary.id === diaryId) {
        this.currentDiary = null
      }
    },
    
    // 添加分类
    addCategory(category) {
      this.categories.push(category)
    },
    
    // 更新分类
    updateCategory(updatedCategory) {
      const index = this.categories.findIndex(category => category.id === updatedCategory.id)
      if (index !== -1) {
        this.categories[index] = updatedCategory
      }
    },
    
    // 删除分类
    deleteCategory(categoryId) {
      this.categories = this.categories.filter(category => category.id !== categoryId)
    },
    
    // 添加标签
    addTag(tag) {
      this.tags.push(tag)
    },
    
    // 更新标签
    updateTag(updatedTag) {
      const index = this.tags.findIndex(tag => tag.id === updatedTag.id)
      if (index !== -1) {
        this.tags[index] = updatedTag
      }
    },
    
    // 删除标签
    deleteTag(tagId) {
      this.tags = this.tags.filter(tag => tag.id !== tagId)
    },
    
    // 设置加载状态
    setLoading(loading) {
      this.loading = loading
    },
    
    // 设置分页信息
    setPagination(page, hasMore) {
      this.currentPage = page
      this.hasMore = hasMore
    },
    
    // 设置筛选条件
    setFilters(filters) {
      this.filters = { ...this.filters, ...filters }
    },
    
    // 清空筛选条件
    clearFilters() {
      this.filters = {
        categoryId: null,
        tagIds: [],
        startDate: null,
        endDate: null,
        keyword: '',
        mood: null
      }
    },
    
    // 重置状态
    resetState() {
      this.diaries = []
      this.currentPage = 1
      this.hasMore = true
    },
    
    // 设置编辑状态
    setEditingDiary(diaryId, date = null) {
      this.editingDiaryId = diaryId
      this.editingDate = date
    },
    
    // 清除编辑状态
    clearEditingState() {
      this.editingDiaryId = null
      this.editingDate = null
    },
    
    // 添加或更新日记（统一方法）
    addOrUpdateDiary(diary) {
      if (diary.id) {
        // 更新现有日记
        this.updateDiary(diary)
      } else {
        // 添加新日记
        this.addDiary(diary)
      }
    }
  }
}) 